package com.lfk.three.view.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lfk.three.R;
import com.lfk.three.con.NetWorkQueue;
import com.lfk.three.item.NetItem;
import com.lfk.three.item.User_Item;
import com.lfk.three.static_info.static_info;
import com.lfk.three.static_info.url_router;
import com.lfk.three.utils.logger.Logger;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText user_name;
    private EditText user_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        user_name = (EditText) findViewById(R.id.login_user_name);
        user_psw = (EditText) findViewById(R.id.login_pass_word);

        findViewById(R.id.login_login_button).setOnClickListener(this);
        findViewById(R.id.login_register_button).setOnClickListener(this);
    }

    private void Login() {
        NetWorkQueue.getInstance().addCallBack("login", new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Logger.e(response);
                User_Item item = new Gson().fromJson(response, User_Item.class);
                static_info.initUserInfo(item);
            }
        });

        HashMap<String, String> temp = new HashMap<>();
        temp.put("pwd", user_psw.getText().toString());
        temp.put("phone", user_name.getText().toString());

        NetWorkQueue.getInstance().
                sendMessage(static_info.POST_DATA,
                        new NetItem("login", url_router.LOGIN_URL, temp));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_register_button:
                Intent register = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(register);
                break;
            case R.id.login_login_button:
                Login();
                break;
        }
    }
}
